<?php 

    // ditulis oleh  @supangat_oy

defined('BASEPATH') OR exit('No direct script access allowed');
defined('SP_BUY') OR exit('APLIKASI EROR SILAHKAN HUBUNGI 083873272419');


class Siswa extends CI_Controller {
	var $judul;
	function __construct(){
		parent::__construct();
		$this->m_config->cek_sesi();
	}
	function index(){
		$data['title']='Import / Export Data Kelas';

		$data['kelas']=$this->db->order_by('XNamaKelas',"ASC")->group_by('XNamaKelas')->select('XNamaKelas')->get('cbt_kelas');
		$this->load->view('head_meta',$data);
		$this->load->view('admin/header',$data);
		
		$this->load->view('admin/importExport/siswa',$data);
		$this->load->view('admin/footer',$data);
		
	}
	function prosesimport(){
		$url=$this->input->get('url');
		$this->load->library('curl');
		$send=[
			'kelas' => $this->input->get('kelas'),
			'ta' => $this->input->get('ta'),
		];
		$siswa=json_decode($this->curl->simple_post($url.'/api/siswa',$send));
		if ($siswa) {
			$data['pesan']='';
			$sukses=$gagal=0;

			foreach ($siswa as $s) {
				$kelas=explode('-', $s->kelas);
				$data['submit']=[
					'XNamaSiswa' => strtoupper($s->nama), 
					'XNIK' => $s->nis, 
					// 'XSesi' => $rowData[0][3], 
					// 'XRuang' => strtoupper($rowData[0][4]), 
					'XKodeKelas' => strtoupper($kelas[0]), 
					'XKodeJurusan' => strtoupper($kelas[1]), 
					'XNamaKelas' => strtoupper($s->kelas), 
					'XJenisKelamin' => strtoupper(substr($s->jk, 0,1)), 
					'XFoto' => '', 
					'XAgama' => strtoupper('ISLAM'), 
					'XSetId' => $s->ta, 
					// 'XKodeSekolah' => $this->input->post('XKodeSekolah'), 
					"LastUpdate" => time()
				];

				if ($this->input->get('nomerUjian') == 'NIS') {
					$data['submit']['XNomerUjian']=$s->nis;
				}

				if ($this->input->get('pwd') == 'nama') {
					if ($this->input->get('balikpwd') == '1') {
						$ppwd=explode(' ', $s->nama);
						krsort($ppwd);
						$data['submit']['XPassword']=strtoupper(str_replace(['',"'"], ['',''], implode('', $ppwd)));
					} else {
						$data['submit']['XPassword']=strtoupper(str_replace([' ',"'"], ['',''], $s->nama));
					}
				} else if ($this->input->get('pwd') == 'ttl') {
					if ($this->input->get('balikpwd') == '1') {
						$ppwd=explode('/', $s->tan_lahir);
						krsort($ppwd);
						$data['submit']['XPassword']=strtoupper(str_replace(['',"'"], ['',''], implode('', $ppwd)));
					} else {
						$data['submit']['XPassword']=strtoupper(str_replace([' ',"'",'/'], ['','',''], $s->tan_lahir));
					}

				}

				if ($this->db->where('XNomerUjian',$s->nis)->get('cbt_siswa')->num_rows() > 0) {
					$this->db->where('XNomerUjian',$s->nis);
		            if ($this->db->update('cbt_siswa',$data['submit'])) {
						$sukses++;			            	
		            } else {
		            	$data['pesan'].='<div class="card-panel	 red white-text">Gagal Upload siswa '.$s->nis.'</div>'; 
		            }
				} else {
		            if ($this->db->insert('cbt_siswa',$data['submit'])) {
						$sukses++;			            	
		            } else {
		            	$data['pesan'].='<div class="card-panel	 red white-text">Gagal Upload siswa '.$s->nis.'</div>'; 
		            }
				}
			}

	        // echo $sukses;
	        if ($sukses > 0) {
	        	$data['pesan'].='<div class="green white-text padding-3">'.$sukses.' Data Siswa sukses di import</div>'; 
	        }
	        if ($gagal > 0 ) {
	        	$data['pesan'].='<div class="red white-text padding-3">'.$gagal.' Data Siswa gagal di import</div>'; 
	        }

	        echo $data['pesan'];

		} else {
			echo "Gagal Mengakses Url tujuan, silahkan cek kembali url yang anda masukan";
		}
	}
}